// pages/wholeHouse/threeSteps/threeSteps.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    stepActive: 1, //步骤激活下标

    minimumArea: null, //面积
    maxmumArea: null,
    // budgetAmount: null, //预算金额
    moneyList: ['0-5万', '5-10万', '10-15万', '15-25万', '25-50万', '50万以上'],

    houseType: ['平常住宅', '复式住宅', '独栋别墅', 'LOFT', '庭 院', '联排住宅'], //你的房屋户型？


    styleList: [{
      name: '新中式',
      select: false
    }, {
      name: '欧式',
      select: false
    }, {
      name: '现代简约',
      select: false
    }, {
      name: '北 欧 ',
      select: false
    }, {
      name: '日式 ',
      select: false
    }, {
      name: '新古典',
      select: false
    }, {
      name: '田园风',
      select: false
    }, {
      name: '混搭风',
      select: false
    }],
    styleIndex: null, //风格下标

    sys_attribute: [], //瞬间类别字典
    active: 0, //tab切换


    sys_budget_amount: [], //房屋预算金额  sys_budget_amount
    activeIndex: 0, //金额下标

    sys_house_type: [], //房屋户型  sys_house_type
    houseIndex: 0, ////房屋下标


    sys_rooms_number: [], //房屋几居室  sys_rooms_number
    roomsIndex: 0, //居室下标


    sys_family_status: [], //房屋家庭状态  sys_family_status
    familyIndex: 0, //家庭状态 下标

    sys_favorite_style: [], //房屋喜欢的风格  sys_favorite_style


  },

  // tabStep  点击单个步骤
  tabStep(e) {
    const stepActive = e.currentTarget.dataset.active
    // console.log(stepActive);

    this.setData({
      stepActive: stepActive
    })
  },
  // 点击预算金额
  clickMoney(e) {
    // console.log(e);
    var index = e.currentTarget.dataset.index
    this.setData({
      activeIndex: index
    })
  },
  // 点击房屋户型
  clickType(e) {
    var index = e.currentTarget.dataset.index
    this.setData({
      houseIndex: index
    })
  },
  //点击几居室
  clickRooms(e) {
    var index = e.currentTarget.dataset.index
    this.setData({
      roomsIndex: index
    })
  },

  //点击家庭状态
  clickFamily(e) {
    var index = e.currentTarget.dataset.index
    this.setData({
      familyIndex: index
    })
  },

  // 点击喜好风格
  clickStyle(e) {
    var index = e.currentTarget.dataset.index
    this.setData({
      [`sys_favorite_style[${index}].select`]: !this.data.sys_favorite_style[index].select
    })
  },



  //监听值变化
  onChange(e) {
    var formName = e.target.dataset.formname
    // var name = `addForm.${formName}`;
    var name = `${formName}`;
    this.setData({
      // [name]: e.detail
      [name]: e.detail
    })
    console.log(formName)
  },
  // 下一步
  next() {
    const stepActive = this.data.stepActive
    if (stepActive == 1) {
      this.setData({
        stepActive: 2
      })
    } else if (stepActive == 2) {
      // wx.navigateTo({
      //   url: '/pages/wholeHouse/list/list',
      // })
      var minimumArea = this.data.minimumArea
      var maxmumArea = this.data.maxmumArea
      var budgetAmount = this.data.sys_budget_amount[this.data.activeIndex].dictValue
      var houseType = this.data.sys_house_type[this.data.houseIndex].dictValue
      var roomsNumber = this.data.sys_rooms_number[this.data.roomsIndex].dictValue
      var familyStatus = this.data.sys_family_status[this.data.familyIndex].dictValue

      var sys_favorite_style = this.data.sys_favorite_style
      var favoriteStyle = []
      sys_favorite_style.forEach((item, i) => {
        if (item.select == true) {
          favoriteStyle.push(JSON.stringify(item.dictCode))
        }
      })
      var data = {
        minimumArea: minimumArea,
        maxmumArea: maxmumArea,
        budgetAmount: budgetAmount,
        houseType: houseType,
        roomsNumber: roomsNumber,
        familyStatus: familyStatus,
        favoriteStyleList: favoriteStyle,
      }

      if (!minimumArea) {
        wx.showToast({
          title: '最小面积！',
          image: '../../../assets/error.png',
          duration: 500,
          mask: true
        })
      } else if (!maxmumArea) {
        wx.showToast({
          title: '最小面积！',
          image: '../../../assets/error.png',
          duration: 500,
          mask: true
        })
      } else if (favoriteStyle.length <= 0) {
        wx.showToast({
          title: '喜好风格！',
          image: '../../../assets/error.png',
          duration: 500,
          mask: true
        })
      } else {
        console.log(data);

        wx.navigateTo({
          url: '/pages/wholeHouse/list/list?data=' + JSON.stringify(data),
        })
      }


    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this

    //获取瞬间类别字典  sys_attribute
    // app.getDict('sys_attribute').then(res => {
    //   let sys_attribute = res.data.data
    //   // console.log('获取瞬间类别字典', sys_attribute);
    //   that.setData({
    //     sys_attribute: sys_attribute,
    //     clickItem: sys_attribute[0]
    //   })
    //   that.getMomentList(sys_attribute[0])
    //   // that.isLeft();
    // })


    //房屋喜欢的风格  sys_favorite_style
    app.getDict('sys_favorite_style').then(res => {
      let sys_favorite_style = res.data.data
      sys_favorite_style.forEach((items, i) => {
        items.url = app.util.config().api + items.url
        items.select = false
      })
      console.log('房屋喜欢的风格', sys_favorite_style);
      that.setData({
        sys_favorite_style: sys_favorite_style,
      })
    })
    //房屋几居室  sys_rooms_number
    app.getDict('sys_rooms_number').then(res => {
      let sys_rooms_number = res.data.data
      sys_rooms_number.forEach((items, i) => {
        items.url = app.util.config().api + items.url
      })
      console.log('房屋几居室', sys_rooms_number);
      that.setData({
        sys_rooms_number: sys_rooms_number,
      })
    })
    //房屋家庭状态  sys_family_status
    app.getDict('sys_family_status').then(res => {
      let sys_family_status = res.data.data
      sys_family_status.forEach((items, i) => {
        items.url = app.util.config().api + items.url
      })
      console.log('房屋家庭状态', sys_family_status);
      that.setData({
        sys_family_status: sys_family_status,
      })
    })
    //房屋预算金额  sys_budget_amount
    app.getDict('sys_budget_amount').then(res => {
      let sys_budget_amount = res.data.data
      console.log('房屋预算金额', sys_budget_amount);
      that.setData({
        sys_budget_amount: sys_budget_amount,
      })
    })
    //房屋户型  sys_house_type
    app.getDict('sys_house_type').then(res => {
      let sys_house_type = res.data.data
      sys_house_type.forEach((items, i) => {
        items.url = app.util.config().api + items.url
      })
      console.log('房屋户型', sys_house_type);
      that.setData({
        sys_house_type: sys_house_type,
      })
    })


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})